package com.sgb.kjwl.viewmodel.home;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.home.ZuLinSearchActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ZuLinSearchModel extends BaseViewModel {

    private ZuLinSearchActivity activity;
    private HomeRepertory repertory;

    public ZuLinSearchModel(ZuLinSearchActivity activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }


    /**
     * 获取本地搜索记录
     */
    public void getSearchHistoryListData() {
        //取出本地存的搜索记录
        String historyWords = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.ZULIN_SEARCH_HISTORY, "");
        if (null != historyWords && !historyWords.isEmpty()) {
            String[] arr = historyWords.split(","); //字符串转数组,  用,分割
            List<String> resultList = new ArrayList<>(arr.length);
            for (String s : arr) {
                resultList.add(s);
            }
            activity.setSearchHistoryData(resultList);
        } else {
            activity.setSearchHistoryData(new ArrayList<>());
        }
    }


    //租赁-搜索热词
    public void getLeaseSearchAreaCode() {
        APIManager.getInstance().getLeaseAPI().getHotKey(repertory.getAreaCode())
                .enqueue(new Callback<BaseEntity<List<String>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<String>>> call, Response<BaseEntity<List<String>>> response) {
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            List<String> list = response.body().getData();
                            if (list != null && list.size() > 10) {
                                list = list.subList(0, 10);
                            }
                            activity.setHotHistoryData(list);
                        }

                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<String>>> call, Throwable t) {
                        t.printStackTrace();
                    }
                });
    }

    /**
     * 获取平台热词推荐内容
     *
     * @param keywords
     */
    public void getCatoryPrompt(String keywords) {
        APIManager.getInstance().getLeaseAPI().getCatoryPrompt(keywords).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                activity.showTipList(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }
}
